package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.BlogSubjectDto;
import com.have.blog.admin.service.AdminBlogSubjectService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.BlogSubjectItemMapper;
import com.have.blog.model.mapper.BlogSubjectMapper;
import com.have.blog.model.models.BlogSubject;
import com.have.blog.model.models.BlogSubjectExample;
import com.have.blog.model.models.BlogSubjectItemExample;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class AdminBlogSubjectServiceImpl implements AdminBlogSubjectService {

    @Autowired
    private BlogSubjectMapper subjectMapper;
    @Autowired
    private BlogSubjectItemMapper itemMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;

    @Override
    public List<BlogSubjectDto> getList(BlogSubjectDto blogSubjectDto) {
        PageHelper.startPage(blogSubjectDto.getPageNum(), blogSubjectDto.getPageSize());
        BlogSubjectExample example = new BlogSubjectExample();
        BlogSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (!StringUtils.isEmpty(blogSubjectDto.getKeyword()) && !StringUtils.isEmpty(blogSubjectDto.getKeyword().trim())) {
            criteria.andSubjectNameLike("%" + blogSubjectDto.getKeyword() + "%");
        }
        example.setOrderByClause("sort desc");
        List<BlogSubject> list = subjectMapper.selectByExample(example);
        List<BlogSubjectDto> resultList = MyBeanUtils.copyListProperties(list, BlogSubjectDto::new);

        List<Integer> picUids = new ArrayList<>();
        resultList.forEach(item ->{
            if (item.getCoverPictureUid() != null) {
                picUids.add(item.getCoverPictureUid());
            }
        });
        if (!CollectionUtils.isEmpty(picUids)) {
            Wrapper<Map<Integer, String>> pictureWrapper = pictureFeignApi.getPicturesUrl(picUids);
            if (pictureWrapper.isSuccess()) {
                Map<Integer, String> picData = pictureWrapper.getData();
                resultList.forEach(item -> {
                    item.setPhotoUrl(picData.get(item.getCoverPictureUid()));
                });
            }
        }

        return resultList;
    }

    @Override
    public Wrapper addSubject(BlogSubjectDto blogSubjectDto) {
        BlogSubjectExample example = new BlogSubjectExample();
        example.createCriteria()
                .andSubjectNameEqualTo(blogSubjectDto.getSubjectName())
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        long count = subjectMapper.countByExample(example);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
        }
        BlogSubject subject = new BlogSubject();
        MyBeanUtils.copyProperties(blogSubjectDto, subject);
        subject.setClickCount(0);
        subject.setCollectCount(0);
        subject.setStatus(BlogStatus.ENABLE.getStatus());
        int insert = subjectMapper.insert(subject);
        if (insert > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    @Override
    public Wrapper editBlogSubject(BlogSubjectDto blogSubjectDto) {
        BlogSubject subject = subjectMapper.selectByPrimaryKey(blogSubjectDto.getUid());
        if (subject == null) {
            return Wrapper.illegal();
        }
        if (!subject.getSubjectName().equals(blogSubjectDto.getSubjectName())) {
            BlogSubjectExample example = new BlogSubjectExample();
            example.createCriteria()
                    .andSubjectNameEqualTo(blogSubjectDto.getSubjectName())
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
            long count = subjectMapper.countByExample(example);
            if (count > 0) {
                return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
            }
        }
        subject.setSubjectName(blogSubjectDto.getSubjectName());
        subject.setSummary(blogSubjectDto.getSummary());
        subject.setCoverPictureUid(blogSubjectDto.getCoverPictureUid());
        subject.setClickCount(blogSubjectDto.getClickCount());
        subject.setCollectCount(blogSubjectDto.getCollectCount());
        subject.setSort(blogSubjectDto.getSort());
        int update = subjectMapper.updateByPrimaryKey(subject);
        if (update > 0){
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deleteBatchSubject(List<Integer> uids) {
        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.illegal();
        }

        BlogSubjectItemExample example = new BlogSubjectItemExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(uids);
        long count = itemMapper.countByExample(example);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.SUBJECT_UNDER_THIS_SORT);
        }

        BlogSubjectExample subjectExample = new BlogSubjectExample();
        subjectExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(uids);
        BlogSubject subject = new BlogSubject();
        subject.setStatus(BlogStatus.DISABLE.getStatus());
        int delete = subjectMapper.updateByExampleSelective(subject, subjectExample);
        if (delete > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }
}
